import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { ServicesApi } from '#/api/reservation/services';

import { useAccess } from '@vben/access';

import { DICT_TYPE, getDictOptions } from '#/utils';

const { hasAccessByCodes } = useAccess();

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: '服务名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入服务名称',
      },
    },
    {
      fieldName: 'category',
      label: '服务分类',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.MRS_SERVICE_CATEGORY, 'number'),
        placeholder: '请选择服务分类',
      },
    },
    {
      fieldName: 'description',
      label: '服务说明',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入服务说明',
      },
    },
    {
      fieldName: 'price',
      label: '服务价格',
      component: 'InputNumber',
      componentProps: {
        prefix: '¥',
        addonAfter: '元',
        placeholder: '请输入服务价格',
      },
    },
    {
      fieldName: 'cycle',
      label: '服务周期',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.MRS_SERVICE_CYCLE, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'status',
      label: '服务状态',
      rules: 'required',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.MRS_SERVICE_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: '服务名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入服务名称',
      },
    },
    {
      fieldName: 'category',
      label: '服务分类',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MRS_SERVICE_CATEGORY, 'number'),
        placeholder: '请选择服务分类',
      },
    },
    {
      fieldName: 'cycle',
      label: '服务周期',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MRS_SERVICE_CYCLE, 'number'),
        placeholder: '请选择服务周期',
      },
    },
    {
      fieldName: 'status',
      label: '服务状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MRS_SERVICE_STATUS, 'number'),
        placeholder: '请选择服务状态',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(
  onActionClick?: OnActionClickFn<ServicesApi.Services>,
): VxeTableGridOptions<ServicesApi.Services>['columns'] {
  return [
    {
      field: 'id',
      title: '主键',
      minWidth: 120,
    },
    {
      field: 'name',
      title: '服务名称',
      minWidth: 120,
    },
    {
      field: 'category',
      title: '服务分类',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MRS_SERVICE_CATEGORY },
      },
    },
    {
      field: 'description',
      title: '服务说明',
      minWidth: 120,
    },
    {
      field: 'price',
      title: '服务价格',
      minWidth: 120,
    },
    {
      field: 'cycle',
      title: '服务周期',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MRS_SERVICE_CYCLE },
      },
    },
    {
      field: 'status',
      title: '服务状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MRS_SERVICE_STATUS },
      },
    },
    {
      field: 'operation',
      title: '操作',
      minWidth: 200,
      align: 'center',
      fixed: 'right',
      headerAlign: 'center',
      showOverflow: false,
      cellRender: {
        attrs: {
          nameField: 'id',
          nameTitle: '店铺服务',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'edit',
            show: hasAccessByCodes(['reservation:services:update']),
          },
          {
            code: 'delete',
            show: hasAccessByCodes(['reservation:services:delete']),
          },
        ],
      },
    },
  ];
}
